package com.changpay.zywy.ui.activity.serivce

import android.view.View
import com.changpay.zywy.R
import com.changpay.zywy.base.BaseActivity
import com.changpay.zywy.base.IView
import com.changpay.zywy.ext.showToast
import com.changpay.zywy.ext.sss
import com.changpay.zywy.http.RetrofitHelper
import kale.ui.view.dialog.EasyDialog
import kotlinx.android.synthetic.main.activity_alloc_machine_detail.*
import kotlinx.android.synthetic.main.title_layout.*
import java.util.HashMap


class AllocMachineDetailActivity : BaseActivity(), IView {
    override fun attachLayoutRes(): Int {
        return R.layout.activity_alloc_machine_detail
    }

    override fun showLoading() {

    }

    override fun hideLoading() {

    }

    override fun showDefaultMsg(msg: String) {
        showToast(msg)
    }

    override fun showMsg(msg: String) {
        showToast(msg)
    }

    override fun showError(errorMsg: String) {
        showToast(errorMsg)
    }


    override fun initData() {


    }

    override fun initView() {
        tv_title.text = "调拨详情"
        iv_back.setOnClickListener { finish() }
        initListener()
    }

    override fun start() {
        val params = HashMap<String, String>()
        params["transfer_id"] = intent.getStringExtra("transfer_id")
        params["key"] = token
        params["client"] = "android"
        RetrofitHelper.service.get_sn_transfer_detail(params).sss(this) {
            val data = it.result
            tv_my_name.text = data.apply_member_name  // 调拨人
            tv_your_name.text = data.member_name
            tv_phone.text = data.member_mobile
            tv_sn_number.text = data.sn_code
            tv_jiju_name.text = data.goods_name
            tv_number.text = data.num
            tv_time.text = data.created_time_date

            if (intent.getBooleanExtra("hide",false))
            {
                if (data.state == "0") {
                    ll_agree_refuse.visibility = View.GONE
                    tv_take_money.visibility = View.INVISIBLE
                    tv_take_money.text = "待审核"
                    tv_take_money.visibility = View.VISIBLE
                } else if (data.state == "1") {
                    ll_agree_refuse.visibility = View.GONE
                    tv_take_money.text = "已通过"
                    tv_take_money.visibility = View.VISIBLE
                }else if (data.state == "2")
                {
                    ll_agree_refuse.visibility = View.GONE
                    tv_take_money.text = "已拒绝"
                    tv_take_money.visibility = View.VISIBLE
                }
            }else
            {
                if (data.state == "0") {
                    ll_agree_refuse.visibility = View.VISIBLE
                    tv_take_money.visibility = View.INVISIBLE

                } else if (data.state == "2") {
                    ll_agree_refuse.visibility = View.GONE
                    tv_take_money.text = "已拒绝"
                    tv_take_money.visibility = View.VISIBLE
                }
            }


        }
    }


    private fun initListener() {

        bt_refuse_all.setOnClickListener {
            agreeOrRefuseAll(false)  // 0 为拒绝调拨
        }

        bt_agree_all.setOnClickListener {
            agreeOrRefuseAll(true) // 1 为同意调拨
        }
    }


    private fun agreeOrRefuseAll(isAgree: Boolean) {
        val message: String
        val state: String
        if (isAgree) {
            message = "确定同意调拨"
            state = "1"
        } else {
            message = "确定拒绝调拨"
            state = "2"
        }
        val builder = EasyDialog.builder(this) // 建立builder对象
        builder.setTitle("提示")
            .setMessage(message)
            // 设置下方的三个按钮
            .setPositiveButton("确定") { dialogInterface, i ->
                val params = HashMap<String, String>()
                params["transfer_id"] = intent.getStringExtra("transfer_id")
                params["state"] = state
                params["key"] = token
                params["client"] = "android"
                RetrofitHelper.service.edit_sn_transfer(params).sss(this)
                {
                    tv_take_money.text = it.result.toString()
                    tv_take_money.visibility = View.VISIBLE
                    ll_agree_refuse.visibility = View.GONE
                    setResult(200)
                }
            }
            .setNegativeButton("取消") { dialog, which -> dialog.dismiss() }
            .setCancelable(true) // 点击空白处可以关闭
        val easyDialog = builder.build<EasyDialog>()
        (easyDialog as EasyDialog).show(supportFragmentManager)
    }
}
